package com.bsdk.install.device.lockLora.v2.cmd;

/**
 * @projectName: lock
 * @package: com.hlslock.lock.cmd
 * @className: CMD8030
 * @author: HansYoung
 * @description: 下发卡用户
 * @date: 2023/12/6 14:16
 * @version: 1.0
 */
public class CMD8034 extends CMD {

    public static class Builder extends CMD {

        private String userType;
        private String password;
        private Long startTime;
        private Long endTime;

        public Builder setUId(String uId) {
            this.uId = uId;
            return this;
        }

        public Builder setCMac(String cMac) {
            this.cMac = cMac;
            return this;
        }

        public Builder setDevId(String devId) {
            this.devId = devId;
            return this;
        }

        public Builder setCmd(String cmd) {
            this.cmd = cmd;
            return this;
        }

        public Builder setPassword(String password) {
            this.password = password;
            return this;
        }

        public Builder setStartTime(Long startTime) {
            this.startTime = startTime;
            return this;
        }

        public Builder setEndTime(Long endTime) {
            this.endTime = endTime;
            return this;
        }

        public CMD bulid() {
            StringBuilder data = new StringBuilder();
            data.append("02168033" + this.userType);
            // password
            StringBuilder pwd = new StringBuilder("FFFFFFFFFFFFFF");
            if (password.length() < 6) {
                throw new IllegalArgumentException("Password less then 6");
            }
            if (password.length() > 14) {
                pwd.replace(0, 13, password);
            } else {
                pwd.replace(0, password.length(), password);
            }
            data.append(pwd);

            data.append(buildStartEndTime(startTime, endTime).toString());
            data.append("03");

            data.append(xor(data.toString()));

            return new CMD(uId, cMac, devId, cmd, data.toString());
        }
    }

    public static Builder getPushPayloadBuider() {
        return new Builder();
    }
}